package cn.cdm.redis;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Slf4j
@Service
@Configuration
public class RedisPoolFactory {
//	private static final Logger log = LoggerFactory.getLogger(RedisPoolFactory.class);

	/**
	 * Redis连接池工厂，用来产生RedisPool连接池对象
	 */

	@Autowired
	RedisConfig redisConfig;

	@Value("${spring.redis.host}")
	private String host;

	@Value("${spring.redis.port}")
	private int port;

	@Value("${spring.redis.password}")
	private String password;

	@Value("${spring.redis.timeout}")
	private int timeout;

	@Value("${spring.redis.jedis.pool.max-idle}")
	private int maxIdle;

	@Value("${spring.redis.jedis.pool.max-active}")
	private int maxActive;

	@Value("${spring.redis.jedis.pool.max-wait}")
	private int maxWait;

	@Bean
	public JedisPool JedisPoolFactory() {
		log.debug("redispool初始化：" + this);
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		poolConfig.setMaxIdle(maxIdle);
		poolConfig.setMaxTotal(maxActive);
		poolConfig.setMaxWaitMillis((long) (maxWait * 1000));
		JedisPool jp = new JedisPool(poolConfig, host, port, timeout * 1000, password);
		return jp;
	}

	@Override
	public String toString() {
		return "RedisPoolFactory{" +
				"host='" + host + '\'' +
				", port='" + port + '\'' +
				", password='" + password + '\'' +
				", timeout=" + timeout +
				", maxIdle=" + maxIdle +
				", maxActive=" + maxActive +
				", maxWait=" + maxWait +
				'}';
	}
}
